package com.seqb.manage.choice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.seqb.common.ResultVo;
import com.seqb.common.mybatis.WrapperHandler;
import com.seqb.exception.BdException;
import com.seqb.manage.choice.dao.ChoiceQuestionDao;
import com.seqb.manage.choice.domain.ChoiceQuestionDO;
import com.seqb.manage.choice.service.ChoiceQuestionService;
import com.seqb.manage.file.dao.FileDao;
import com.seqb.manage.file.service.FileService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Arrays;
import java.util.Map;

/**
 * @author lilinshen
 * @title 选择题目表
 * @description 请填写相关描述
 * @date 2019-11-27
 */
@Service
public class ChoiceQuestionServiceImpl implements ChoiceQuestionService {
    @Autowired
    private FileDao fileDao;
    @Autowired
    private FileService fileService;
    @Autowired
    private ChoiceQuestionDao choiceQuestionDao;

    @Override
    public ChoiceQuestionDO get(Long id) {
        return choiceQuestionDao.selectById(id);
    }

    @Override
    public Page<ChoiceQuestionDO> list(Map<String, Object> map) {
        if (null != map && null != map.get("pageNumber") && null != map.get("pageSize")) {
            PageHelper.startPage(Integer.valueOf(map.get("pageNumber").toString()), Integer.valueOf(map.get("pageSize").toString()));
        } else {
            PageHelper.startPage(1, Integer.MAX_VALUE);
        }
        QueryWrapper queryWrapper = WrapperHandler.genQueryWrapper(map, ChoiceQuestionDO.class);
        queryWrapper.orderByDesc("id");
        return (Page<ChoiceQuestionDO>) choiceQuestionDao.selectList(queryWrapper);
    }

    @Override
    public int count(Map<String, Object> map) {
        QueryWrapper queryWrapper = WrapperHandler.genQueryWrapper(map, ChoiceQuestionDO.class);
        return choiceQuestionDao.selectCount(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVo save(ChoiceQuestionDO choiceQuestion) {
        try {
            //保存题目操作
            String name = choiceQuestion.getName();
            Long subjectId = choiceQuestion.getSubjectId();
            if (null == subjectId || StringUtils.isEmpty(name)) {
                return ResultVo.error("参数不足！");
            }
            int insert = choiceQuestionDao.insert(choiceQuestion);
            if (insert <= 0) {
                throw new BdException("操作失败！");
            }
            //更新附件表数据
            Long id = choiceQuestion.getId();
            String ids = choiceQuestion.getIds();
            ResultVo resultVo = fileService.updateIds(ids, id, "ChoiceQuestion", "Attachment");
            if (null != resultVo.get("fileUrl")) {
                ChoiceQuestionDO choiceQuestionDO = new ChoiceQuestionDO();
                choiceQuestionDO.setId(choiceQuestion.getId());
                choiceQuestionDO.setFileUrl(String.valueOf(resultVo.get("fileUrl")));
                int update = choiceQuestionDao.updateById(choiceQuestionDO);
                if (update <= 0) {
                    throw new BdException("操作失败！");
                }
            }
            return ResultVo.ok();
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return ResultVo.error();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVo update(ChoiceQuestionDO choiceQuestion) {
        try {
            //更新题目操作
            Long id = choiceQuestion.getId();
            String name = choiceQuestion.getName();
            Long subjectId = choiceQuestion.getSubjectId();
            if (null == id || null == subjectId || StringUtils.isEmpty(name)) {
                return ResultVo.error("参数不足！");
            }
            int update = 0;
            String clearField = choiceQuestion.getClearField();
            if (StringUtils.isEmpty(clearField)) {
                update = choiceQuestionDao.updateById(choiceQuestion);
            } else {
                UpdateWrapper updateWrapper = WrapperHandler.genUpdateWrapper(choiceQuestion);
                updateWrapper.eq("id", id);
                update = choiceQuestionDao.update(choiceQuestion, updateWrapper);
            }

            //更新附件表数据
            String ids = choiceQuestion.getIds();
            ResultVo resultVo = fileService.updateIds(ids, id, "ChoiceQuestion", "Attachment");
            if (null != resultVo.get("fileUrl")) {
                ChoiceQuestionDO choiceQuestionDO = new ChoiceQuestionDO();
                choiceQuestionDO.setId(choiceQuestion.getId());
                choiceQuestionDO.setFileUrl(String.valueOf(resultVo.get("fileUrl")));
                update = choiceQuestionDao.updateById(choiceQuestionDO);
            }
            if (update <= 0) {
                throw new BdException("操作失败！");
            }
            return ResultVo.ok();
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return ResultVo.error();
    }

    @Override
    @Transactional
    public int remove(Long id) {
        return choiceQuestionDao.deleteById(id);
    }

    @Override
    @Transactional
    public int batchRemove(Long[] ids) {
        return choiceQuestionDao.deleteBatchIds(Arrays.asList(ids));
    }

}
